package com.fms.gui.templateCreator.components.selector;

import java.io.File;

import javax.swing.JFileChooser;

import com.fms.model.factory.parts.IFileNameSupplier;
import com.fms.model.properties.AbstractProperties;

public class XLSFileSelector implements IFileNameSupplier
{
	private AbstractProperties	props;

	public XLSFileSelector(AbstractProperties _props)
	{
		this.props = _props;
	}
	
	
	private static final String	SAVE_FILE	= "gui.selector.save";

	public String getFile()
	{
		
		JFileChooser chooser = new JFileChooser();
		if(props.containsKey(SAVE_FILE))
		{
			chooser.setCurrentDirectory(new File(props.getProperty(SAVE_FILE)));
		}
		chooser.addChoosableFileFilter(new XLSFileFilter());
		
		int result = chooser.showSaveDialog(null);
		if (result == JFileChooser.APPROVE_OPTION) 
		{
            File file = chooser.getSelectedFile();
            if(!file.getAbsolutePath().toLowerCase().endsWith(".xls"))
            {
            	file = new File(file.getAbsolutePath() + ".xls");            	
            }
            props.put(SAVE_FILE, file.getPath());
        	props.save();
            return file.getAbsolutePath();            
        } 
		else 
        {
			return null;
        }

	}
}
